package com.example.cpu100demo;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.concurrent.*;

/**
 * @author chaird
 * @create 2022-04-25 23:33
 */
@Service
public class Cpu100Service {

  private ExecutorService executorService =
      new ThreadPoolExecutor(
          300,
          300,
          2L,
          TimeUnit.SECONDS,
          new LinkedBlockingQueue<Runnable>(3),
          Executors.defaultThreadFactory(),
          new ThreadPoolExecutor.AbortPolicy());

  public static String name = "cbeann";

  public void cpu100() {
    int a = 10;
    while (a < 100) {
      System.out.println(LocalDateTime.now().toString());
    }
  }

  public void thread20() {

    for (int i = 0; i < 300; i++) {

      executorService.submit(
          () -> {
            System.out.println(Thread.currentThread().getName() + "in");
            try {
              int a = 10;
              while (a < 100) {
                try {
                  TimeUnit.SECONDS.sleep(1);
                } catch (Exception e) {
                  e.printStackTrace();
                } finally {
                }
                Student student = new Student();
                System.out.println(student);
              }

            } catch (Exception e) {
              e.printStackTrace();
            } finally {
            }
            System.out.println(Thread.currentThread().getName() + "out");
          });
    }
  }
}
